package com.jeady.timetodo.activities.task

import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.compose.ui.unit.dp
import androidx.room.Room
import com.jeady.jxcompose.TipDisplay
import com.jeady.jxcompose.showTip
import com.jeady.timetodo.database.AppDatabase
import com.jeady.timetodo.database.Task
import com.jeady.timetodo.utils.timeFormat
import kotlin.concurrent.thread

class TaskDetailActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: android.os.Bundle?) {
        super.onCreate(savedInstanceState)
        val taskId = intent.getLongExtra("taskId", 0)
        setContent {
            TaskDetailUI(taskId)
            TipDisplay()
        }
    }

    @Composable
    fun TaskDetailUI(taskId: Long) {
        val context = LocalContext.current
        var task: Task? by remember {
            mutableStateOf(Task())
        }
        Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
            task?.let {
                TaskInfo(it)
            } ?: run {
                Text("指定ID($taskId)的记录不存在")
            }
        }
        LaunchedEffect(context) {
            if (taskId <= 0) {
                showTip("无效的任务ID $taskId", confirmButtonText = "返回任务列表") {
                    context.startActivity(android.content.Intent(context, TaskActivity::class.java))
                }
            } else {
                thread {
                    val db =
                        Room.databaseBuilder(context, AppDatabase::class.java, "timetodo").build()
                    task = db.taskDao().getTasksByTaskId(taskId).firstOrNull()
                    db.close()
                }
            }
        }
    }

    @Composable
    fun TaskInfo(task: Task) {
        Surface(Modifier.padding(5.dp), shape = RoundedCornerShape(10.dp), shadowElevation = 1.dp) {
            LazyColumn(Modifier.padding(5.dp) ,verticalArrangement = Arrangement.spacedBy(5.dp)) {
                item{ TaskInfoItem("任务ID" to task.taskId.toString()) }
                item{ TaskInfoItem("任务名称" to task.title) }
                item{ TaskInfoItem("任务描述" to task.description) }
                item{ TaskInfoItem("任务进度" to task.state) }
                item{ TaskInfoItem("开始时间" to task.startTime.timeFormat()) }
                item{ TaskInfoItem("间隔时间" to task.interval.toString()) }
                item{ TaskInfoItem("结束时间" to task.endTime.timeFormat()) }
                item{ TaskInfoItem("创建时间" to task.createAt) }
                item{ TaskInfoItem("更新时间" to task.updateAt) }
            }
        }
    }

    @Composable
    fun TaskInfoItem(info: Pair<String, String>){
        Row(Modifier.fillMaxWidth().height(50.dp), verticalAlignment = Alignment.CenterVertically) {
            Box(Modifier.fillMaxHeight().weight(1f).background(Color(0xffa0c0c0)),
                contentAlignment = Alignment.Center
            ){
                Text(
                    info.first,
                    textAlign = TextAlign.Center
                )
            }
            Box(Modifier.fillMaxHeight().weight(1f).background(Color(0xff80a0a0)),
                contentAlignment = Alignment.Center
            ){
                Text(
                    info.second,
                    color = Color.LightGray,
                    textAlign = TextAlign.Center
                )
            }
        }
    }

    @Preview(showBackground = true, fontScale = 2f)
    @Composable
    fun TaskDetailUIPreview(@PreviewParameter(TaskProvider::class) task: Task) {
        TaskInfo(task)
    }
}
class StringProvider : PreviewParameterProvider<String> {
    override val values: Sequence<String>
        get() = listOf("你要上班啦", "你要上学啦").asSequence()
}

class TaskIdProvider : PreviewParameterProvider<Long> {
    override val values: Sequence<Long>
        get() = listOf(1715995291981, 1715995292759, 1715995408430).asSequence()
}

class TaskProvider : PreviewParameterProvider<Task> {
    override val values: Sequence<Task>
        get() = listOf(
            Task(
                id = 1,
                taskId = 1715995291981,
                type = "LIGHT_ON",
                title = "定时打开手电筒",
                description = "定时 13 s",
                state = "已完成",
                startTime = 1715995291982,
                interval = 13000,
                endTime = 1715995304982,
                createAt = "2024-05-18 09:21:31",
                updateAt = "2024-05-18 09:21:45"
            ),
            Task(
                id = 2,
                taskId = 1715995292759,
                type = "LIGHT_OFF",
                title = "定时关闭手电筒",
                description = "定时 13 s",
                state = "已完成",
                startTime = 1715995292759,
                interval = 13000,
                endTime = 1715995305759,
                createAt = "2024-05-18 09:21:32",
                updateAt = "2024-05-18 09:21:45"
            ),
            Task(
                id = 3,
                taskId = 1715995408430,
                type = "LIGHT_ON",
                title = "定时打开手电筒",
                description = "定时 0 s",
                state = "已完成",
                startTime = 1715995408431,
                interval = 0,
                endTime = 1715995408431,
                createAt = "2024-05-18 09:23:28",
                updateAt = "2024-05-18 09:23:28"
            ),
            Task(
                id = 4,
                taskId = 1715995409115,
                type = "LIGHT_OFF",
                title = "定时关闭手电筒",
                description = "定时 0 s",
                state = "已完成",
                startTime = 1715995409115,
                interval = 0,
                endTime = 1715995409115,
                createAt = "2024-05-18 09:23:29",
                updateAt = "2024-05-18 09:23:29"
            ),
            Task(
                id = 5,
                taskId = 1715995940620,
                type = "LIGHT_ON",
                title = "定时打开手电筒",
                description = "定时 0 s",
                state = "已完成",
                startTime = 1715995940620,
                interval = 0,
                endTime = 1715995940620,
                createAt = "2024-05-18 09:32:20",
                updateAt = "2024-05-18 09:32:20"
            ),
            Task(
                id = 6,
                taskId = 1715995941966,
                type = "LIGHT_OFF",
                title = "定时关闭手电筒",
                description = "定时 0 s",
                state = "已完成",
                startTime = 1715995941966,
                interval = 0,
                endTime = 1715995941966,
                createAt = "2024-05-18 09:32:21",
                updateAt = "2024-05-18 09:32:21"
            ),
            Task(
                id = 7,
                taskId = 1715996131356,
                type = "LIGHT_OFF",
                title = "定时关闭手电筒",
                description = "定时 0 s",
                state = "已完成",
                startTime = 1715996131356,
                interval = 0,
                endTime = 1715996131356,
                createAt = "2024-05-18 09:35:31",
                updateAt = "2024-05-18 09:35:31"
            ),
            Task(
                id = 8,
                taskId = 1715996148853,
                type = "LIGHT_ON",
                title = "定时打开手电筒",
                description = "定时 0 s",
                state = "已完成",
                startTime = 1715996148853,
                interval = 0,
                endTime = 1715996148853,
                createAt = "2024-05-18 09:35:48",
                updateAt = "2024-05-18 09:35:48"
            ),
            Task(
                id = 9,
                taskId = 1715996149329,
                type = "LIGHT_OFF",
                title = "定时关闭手电筒",
                description = "定时 0 s",
                state = "已完成",
                startTime = 1715996149329,
                interval = 0,
                endTime = 1715996149329,
                createAt = "2024-05-18 09:35:49",
                updateAt = "2024-05-18 09:35:49"
            ),
            Task(
                id = 10,
                taskId = 1715997062233,
                type = "LIGHT_ON",
                title = "定时打开手电筒",
                description = "定时 0 s",
                state = "已完成",
                startTime = 1715997062234,
                interval = 0,
                endTime = 1715997062234,
                createAt = "2024-05-18 09:51:02",
                updateAt = "2024-05-18 09:51:02"
            ),
            Task(
                id = 11,
                taskId = 1715997062500,
                type = "LIGHT_OFF",
                title = "定时关闭手电筒",
                description = "定时 0 s",
                state = "已完成",
                startTime = 1715997062500,
                interval = 0,
                endTime = 1715997062500,
                createAt = "2024-05-18 09:51:02",
                updateAt = "2024-05-18 09:51:02"
            ),
            Task(
                id = 12,
                taskId = 1715998449607,
                type = "LIGHT_ON",
                title = "定时打开手电筒",
                description = "定时 18 s",
                state = "已完成",
                startTime = 1715998550932,
                interval = 18680,
                endTime = 1715998569612,
                createAt = "2024-05-18 10:15:50",
                updateAt = "2024-05-18 10:16:09"
            ),
            Task(
                id = 13,
                taskId = 1715998462834,
                type = "LIGHT_OFF",
                title = "定时关闭手电筒",
                description = "定时 31 s",
                state = "已完成",
                startTime = 1715998551037,
                interval = 31803,
                endTime = 1715998582840,
                createAt = "2024-05-18 10:15:51",
                updateAt = "2024-05-18 10:16:22"
            ),
            Task(
                id = 14,
                taskId = 1715998966238,
                type = "LIGHT_ON",
                title = "定时打开手电筒",
                description = "定时 120 s",
                state = "已完成",
                startTime = 1715998966238,
                interval = 120000,
                endTime = 1715999086238,
                createAt = "2024-05-18 10:22:46",
                updateAt = "2024-05-18 10:24:46"
            ),
            Task(
                id = 15,
                taskId = 1715998967661,
                type = "LIGHT_OFF",
                title = "定时关闭手电筒",
                description = "定时 120 s",
                state = "已完成",
                startTime = 1715998967661,
                interval = 120000,
                endTime = 1715999087661,
                createAt = "2024-05-18 10:22:47",
                updateAt = "2024-05-18 10:24:47"
            ),
            Task(
                id = 16,
                taskId = 1716003514034,
                type = "LIGHT_ON",
                title = "定时打开手电筒",
                description = "定时 16 s",
                state = "已完成",
                startTime = 1716003514034,
                interval = 16000,
                endTime = 1716003530034,
                createAt = "2024-05-18 11:38:34",
                updateAt = "2024-05-18 11:38:50"
            )
            ).asSequence()
}
